using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using Q.Framework.WebCode;
using System.Data.Objects.DataClasses;
using Q.Framework.Data;
using System.Data.Objects;

namespace Q.Framework.WebCode
{
    abstract public class BasePageCrud<T, C> : BasePageCrud<T>
        where C : DAO<T>
    {
        private C controller;

        public C Controller
        {
            get
            {
                if (controller == null)
                    controller = ControllerBase.Create<C>();

                return controller;
            }
        }

        protected override void Delete(object sender, EventArgs e)
        {
            Controller.Delete(Business);
        }

        protected override void Save(object sender, EventArgs e)
        {
            SetBusinessProperties();
            Controller.Save(Business);
        }
    }

    abstract public class BasePageCrud<T> : BasePageCrud
    {
        private T business;

        public T Business
        {
            get
            {
                if (business == null)
                {
                    if (Request.QueryString["ID"] != null)
                        business = DAOGeneric.GetById<T>(Convert.ToInt32(Request.QueryString["ID"]));
                    else
                        business = Activator.CreateInstance<T>();
                }

                return business;
            }
        }

        protected override void OnInit(EventArgs e)
        {
            base.OnInit(e);

            ((CrudMasterPageBase)Master).DoAfterSave += new EventHandler(AfterSave);
        }

        protected override void OnLoad(EventArgs e)
        {
            base.OnLoad(e);
            if (!IsPostBack && IsUpdate)
                LoadControls();
        }

        public virtual void LoadControls() { }

        public virtual void SetBusinessProperties() { }

        protected override void Save(object sender, EventArgs e)
        {
            SetBusinessProperties();
            new DAO<T>().Save(Business);
        }

        protected void AfterSave(object sender, EventArgs e)
        {
            if (Page.Request.QueryString != null && Page.Request.QueryString["FastCrud"] != null && Page.Request.QueryString["FastCrud"] == "S")
            {
                var ClientOkButtonID = Page.Request.QueryString["ClientOkButtonID"];
                var ClientKeyFieldID = Page.Request.QueryString["ClientKeyFieldID"];
                var KeyFieldFastCrud = Page.Request.QueryString["KeyFieldFastCrud"];
                var valueTextBox = typeof(T).GetProperty(KeyFieldFastCrud).GetValue(Business, null).ToString();

                this.Page.ClientScript.RegisterClientScriptBlock(this.Page.GetType(), "onLoad", String.Format("window.parent.document.getElementById('{0}').value = '{1}'; window.parent.document.getElementById('{2}').click(); ", ClientKeyFieldID, valueTextBox, ClientOkButtonID), true);
            }
        }

        protected override void Delete(object sender, EventArgs e)
        {
            new DAO<T>().Delete(Business);
        }

        public Boolean IsInsert
        {
            get { return Request.QueryString["ID"] == null; }
        }

        public Boolean IsUpdate
        {
            get { return !IsInsert; }
        }
    }

    abstract public class BasePageCrud : PageBase
    {
        public CrudMasterPageBase MasterCrud
        {
            get
            {
                return (CrudMasterPageBase)Master;
            }
        }



        protected override void OnInit(EventArgs e)
        {
            if (!this.DesignMode)
            {
                base.PageBasePage_Load(e);

                ((CrudMasterPageBase)Master).Save += Save;
                ((CrudMasterPageBase)Master).Delete += Delete;
                ((CrudMasterPageBase)Master).Cancel += Cancel;
                ((CrudMasterPageBase)Master).New += New;
                ((CrudMasterPageBase)Master).RedirectToSearchPage += RedirectToSearchPage;
            }

            base.OnInit(e);
        }

        abstract protected void Save(object sender, EventArgs e);

        abstract protected void Delete(object sender, EventArgs e);

        virtual protected void Cancel(object sender, EventArgs e) { Redirect(GetPage()); }

        virtual protected void New(object sender, EventArgs e) { Redirect(GetPage()); }

        virtual protected void RedirectToSearchPage(object sender, EventArgs e)
        {
            if (Session["PageSearch"] != null)
            {
                Redirect(Session["PageSearch"].ToString());
                Session["PageSearch"] = null;
            }
            else
                Redirect("PageSearch.aspx"); // Verificar uma forma melhor de fazer.. GMF
        }
    }
}